/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Views.GestionRestaurateur;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;
import javax.swing.border.Border;

/**
 *
 * @author ibrahim
 */
public class PreferenceDialog extends JDialog {
    private JButton okButton;
    private JButton cancelButton;
    private JTextField userField;
    private JPasswordField passField;
    private JSpinner portSpinner;
    private SpinnerNumberModel jspinnerModel;
    private PreferenceListener preflistener;
    public PreferenceDialog(JFrame parent){
    super(parent,"Preferences",false);   
    setSize(270,200);
    setLocationRelativeTo(parent);
    okButton = new JButton("Ok");
    cancelButton =  new JButton("Cancel");
    userField = new JTextField(10);
    passField = new JPasswordField(10);
    passField.setEchoChar('*');
    jspinnerModel = new SpinnerNumberModel(3306,0,9999,1);
    portSpinner = new JSpinner(jspinnerModel);
    
    LayoutControls();
    
    okButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            int port = (Integer)jspinnerModel.getValue();
            String user = userField.getText();
            char[] password = passField.getPassword();
            System.out.println(user + " " + new String(password));
            if(preflistener!=null){
               preflistener.preferenceSet(user, new String(password), port);
            }
            setVisible(false);
        }
    });
    
    cancelButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            
            setVisible(false);
        }
    });
    
   
        
    
    }
    private void  LayoutControls(){
    JPanel controlPanel = new JPanel();
    JPanel buttonsPanel = new JPanel();
    int space =15;
    Border titleBorder = BorderFactory.createTitledBorder("Database Preferences");
    Border spaceBorder = BorderFactory.createEmptyBorder(space,space,space,space);
    controlPanel.setBorder(BorderFactory.createCompoundBorder(spaceBorder, titleBorder));
    //buttonsPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    controlPanel.setLayout(new GridBagLayout());
    GridBagConstraints gc = new GridBagConstraints ();
    Insets rightPadding = new Insets(0,0,0,15);
    Insets noPadding = new Insets(0,0,0,0);
    gc.weightx=1;
    gc.weighty=1;
    gc.fill = GridBagConstraints.NONE;
    ///First Row///
    gc.gridy=0;
    gc.gridx=0;
    gc.anchor = GridBagConstraints.EAST;
    gc.insets = rightPadding;
    controlPanel.add(new JLabel("Username: "),gc);
    gc.gridx++;
    gc.anchor = GridBagConstraints.WEST;
    gc.insets = noPadding;
    controlPanel.add(userField,gc);
    ///Next Row///
    gc.gridy++;
    gc.gridx=0;
    gc.anchor = GridBagConstraints.EAST;
    gc.insets = rightPadding;
    controlPanel.add(new JLabel("Password: "),gc);
    gc.gridx++;
    gc.anchor = GridBagConstraints.WEST;
    gc.insets = noPadding;
    controlPanel.add(passField,gc);
    ////Next Row//////
    gc.gridy++;
    gc.gridx=0;
    gc.anchor = GridBagConstraints.EAST;
    gc.insets = rightPadding;
    controlPanel.add(new JLabel("Port: "),gc);
    gc.gridx++;
    gc.anchor = GridBagConstraints.WEST;
    gc.insets = noPadding;
    controlPanel.add(portSpinner,gc);
    ///Next Row (Buttons Panel////
    buttonsPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
    buttonsPanel.add(okButton,gc);
    buttonsPanel.add(cancelButton,gc);
    Dimension btnSize = cancelButton.getPreferredSize();
    okButton.setPreferredSize(btnSize);
    //Add subpanels to dialog///
    setLayout(new BorderLayout());
    add(controlPanel,BorderLayout.CENTER );
    add(buttonsPanel,BorderLayout.SOUTH );
        }
     public void setPreferencelistener(PreferenceListener listener){
        this.preflistener=listener;
    }
     public void setDefaultPreferences(String username, String password, int port){
        this.userField.setText(username);
        this.passField.setText(password);
        this.portSpinner.setValue(port);
     }
}

